/*
 * Copyright 2012 Gayan Perera
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package gap.maven.script.components;

import gap.maven.script.components.api.ArtifactFileFactory;
import gap.maven.script.components.api.POMArtifactFile;
import gap.maven.script.components.api.ScriptContext;

import java.util.HashMap;

import org.apache.maven.project.MavenProject;

public class DefaultScriptContext implements ScriptContext {
    private final HashMap<String, Object> objectMap = new HashMap<String, Object>();
    private final MavenProject project; 
    
    public DefaultScriptContext(ComponentContext context, MavenProject project) {
        this.project = project;
        objectMap.put(ArtifactFileFactory.SCRIPT_CONTEXT_KEY, new ArtifactFileFactoryImpl(context));
    }

    /* (non-Javadoc)
     * @see gap.maven.script.components.ScriptContext#lookup(java.lang.String)
     */
    @SuppressWarnings("unchecked")
    public <T> T lookup(String key) {
        return (T) objectMap.get(key);
    }
    
    /* (non-Javadoc)
     * @see gap.maven.script.components.ScriptContext#getProject()
     */
    public POMArtifactFile getProject() {
        if(project == null) {
            throw new IllegalStateException("No maven project exist.");
        }
        
        return ((ArtifactFileFactory)objectMap.get(ArtifactFileFactory.SCRIPT_CONTEXT_KEY)).createPOMFile(project);
    }
    
    /* (non-Javadoc)
     * @see gap.maven.script.components.ScriptContext#hasProjectPOM()
     */
    public boolean hasProjectPOM() {
        return (project != null);
    }
}
